<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class DownLoad extends CApiComponent
{
    public $_userId;            public $fileId;        
    
    public function __construct ()
    {
        parent::init();
        $this->result = array();
        $this->result["state"] = false;
        $this->result["msg_code"] = 0;
        $this->result["msg"] = "下载错误";
    }

    
    public function invoke(){
        if (!isset($this->fileId)){
            $this->handleResult(false, 0, "下载错误");
            return;
        }

                $fileDetail = UserFile::model()->findByPk($this->fileId);
        if (empty($fileDetail) || $fileDetail["is_deleted"] == 1){
            $this->handleResult(false, 0, "没有权限下载");
            return;
        }
        
        if (!isset($this->_userId) || $fileDetail['user_id'] != intval($this->_userId)){
            $this->handleResult(false, 0, "没有权限下载");
            return;
        }
        
        $file_name = $fileDetail['file_name'];
        $version_id = $fileDetail['version_id'];

                $file_version = FileVersion::model()->findByPk($version_id);
        $signature = $file_version["file_signature"];
        $content_type = $file_version["mime_type"];

                $file_path = DOCUMENT_ROOT_BLOCK . CUtils::getPathBySplitStr ( $signature );
        if (file_exists ( $file_path ) === false) {
            throw new ApiException("File don't exists.");
        }
                                if (headers_sent ()) {
            $this->handleResult(false, 0, "下载错误");
            return;
        }
        CUtils::outContent($file_path, $content_type, $file_name);
    }



    
    public function handleException ($exception)
    {
        $this->handleResult(false, 0, "下载错误");
    }

    
    public function handleError($code, $message, $file, $line) {
        $this->handleResult(false, 0, "下载错误");
    }
}
?>